package cjyyp.day02;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class 交替打印12 {
    static  Object lock=new Object();
    static int count=1;

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    while (true){
                        if(count%2!=0){
                            if(count>=100){
                                return;
                            }
                            log.debug("{}",count);
                            count++;
                        }else {
                            try {
                                lock.notify();
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    while (true){
                        if(count%2==0){
                            log.debug("{}",count);
                            count++;
                        }else {
                            try {
                                lock.notify();
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        if(count==101){
                            lock.notifyAll();
                            return;
                        }
                    }
                }
            }
        });

        t1.start();
        t2.start();

    }
}
